import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
 * 此处省略N行代码，当我看到这些代码时
 * 瞬间！！血压280 把这尊大佛调去了别的组，别来嚯嚯我了！！！
 */
public class TypeChangeUtil {

    private static final Pattern AMOUNT_PATTERN = Pattern.compile("^(0|[1-9]\\d{0,11})\\.(\\d\\d)$");
    private static final char[] RMB_NUMS = "零壹贰叁肆伍陆柒捌玖".toCharArray();
    private static final String[] UNITS = {"元", "角", "分", "整"};
    private static final String[] U1 = {"", "拾", "佰", "仟"};
    private static final String[] U2 = {"", "万", "亿"};


    public static String getIdType2(String bankIdType, String nationalityCode)
    {
        String idtype = null;
        if("4".equals(bankIdType)){
            if("446".equals(nationalityCode)){
                idtype =  "516";
            } else if ("344".equals(nationalityCode)) {
                idtype =  "516";
            } else if ("158".equals(nationalityCode)) {
                idtype =  "511";
            }
        }else if("31".equals(bankIdType)){
            if("446".equals(nationalityCode)){
                idtype =  "563";
            } else if ("344".equals(nationalityCode)) {
                idtype =  "563";
            } else if ("158".equals(nationalityCode)) {
                idtype =  "564";
            }
        }
        return idtype;
    }


    public static String tranPayDateCHN(String payDate, String payInterval)
    {
        if ("12".equals(payInterval))
        {
            payDate = "每年" + payDate.substring(5, 7) + "月" + payDate.substring(8, 10) + "日";
        } else
        {
            payDate = "";
        }
        return payDate;
    }

    /**
     * 缴费方式
     */
    public static String transferPayMethodCHN(String payMethod)
    {
        String temp = trimStr(payMethod);
        switch (temp)
        {
            case "12":
                return "年缴";
            case "1":
                return "月缴";
            case "0":
                return "趸缴";
            default:
                return "";
        }
    }

    public static String trimStr(String str)
    {
        if (str == null)
        {
            str = "";
        } else
        {
            str = str.trim();
        }
        return str;
    }

    /**
     * 将金额（整数部分等于或少于 12 位，小数部分 2 位）转换为中文大写形式.
     *
     * @param amount 金额数字分
     * @return 中文大写
     */
    public static String convertToChinese(String amount) throws IllegalArgumentException
    {
        // 去掉分隔符
        amount = amount.replace(",", "");
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        amount = decimalFormat.format(new BigDecimal(amount));
        if (amount.equals("0.00"))
        {
            return "零";
        }
        Matcher matcher = AMOUNT_PATTERN.matcher(amount);
        if (!matcher.find())
        {
            throw new IllegalArgumentException("输入金额有误.");
        }
        /*整数部分*/
        String integer = matcher.group(1);
        /*小数部分*/
        String fraction = matcher.group(2);
        String result = "";
        if (!integer.equals("0"))
        {
            result += integer2rmb(integer) + UNITS[0];
        }
        if (fraction.equals("00"))
        {
            result += UNITS[3];
        } else if (fraction.startsWith("0") && integer.equals("0"))
        {
            result += fraction2rmb(fraction).substring(1);
        } else
        {
            result += fraction2rmb(fraction);
        }
        return "人民币" + result;
    }
    /**
     * 小数部分处理成中文
     */
    public static String fraction2rmb(String fraction)
    {
        // 角
        char jiao = fraction.charAt(0);
        // 分
        char fen = fraction.charAt(1);
        return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS[1] : ""))
                + (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS[2] : "");
    }

    /**
     * 整数部分处理成中文
     */
    public static String integer2rmb(String integer)
    {
        StringBuilder buffer = new StringBuilder();
        int i, j;
        for (i = integer.length() - 1, j = 0; i >= 0; i--, j++)
        {
            char n = integer.charAt(i);
            if (n == '0')
            {
                // 当 n 是 0 且 n 的右边一位不是 0 时，插入[零]
                if (i < integer.length() - 1 && integer.charAt(i + 1) != '0')
                {
                    buffer.append(RMB_NUMS[0]);
                }
                // 插入[万]或者[亿]
                if (j % 4 == 0)
                {
                    if (i > 0 && integer.charAt(i - 1) != '0' || i > 1 && integer.charAt(i - 2) != '0'
                            || i > 2 && integer.charAt(i - 3) != '0')
                    {
                        buffer.append(U2[j / 4]);
                    }
                }
            } else
            {
                if (j % 4 == 0)
                {
                    // 插入[万]或者[亿]
                    buffer.append(U2[j / 4]);
                }
                // 插入[拾]、[佰]或[仟]
                buffer.append(U1[j % 4]);
                // 插入数字
                buffer.append(RMB_NUMS[n - '0']);
            }
        }
        return buffer.reverse().toString();
    }

    public static String getAddressResolution(String address,String detailed,String SaleType)
    {
        if("5".equals(SaleType)){
            // 工行手机银行
            address = address.replace("（市辖区）", "");
            if (address.startsWith("北京市") || address.startsWith("天津市") || address.startsWith("上海市") || address.startsWith("重庆市")) {
                address = address.substring(3);
            }
        }else if("1".equals(SaleType)){
            if (address.startsWith("北京市") || address.startsWith("天津市") || address.startsWith("上海市") || address.startsWith("重庆市")) {
                address = address.replace("省（市、区）", "");
                address = address.substring(3);
            }else {
                address = address.replace("（市、区）", "");
            }
            address = address.replace("（市辖区）市", "");
            address = address.replace("县（区）", "");
        } else {
            address = address.replace("（市辖区）", "");
        }
        if (address.startsWith("北京市") || address.startsWith("天津市") || address.startsWith("上海市") || address.startsWith("重庆市")) {
            address = address.substring(0, 3) + "市辖区" + address.substring(3);
        }
        String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)(?<add>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province = null, city = null, county = null;
        while(m.find()){
            province = m.group("province");
            if (province.equals("北京市") || province.equals("天津市") || province.equals("上海市") || province.equals("重庆市")) {
                city = province;
                county = m.group("city");
                if (county.split("区").length > 1) {
                    county = county.substring(0, county.indexOf("区") + 1);
                } else {
                    county = m.group("county");
                    if (county != null && county.split("区").length > 1) {
                        county = county.substring(0, county.indexOf("区") + 1);
                    }
                }
            } else {
                city = m.group("city");

                county = m.group("county");
                if (county != null && !"".equals(county)) {
                    if (county.split("市").length > 1 && county.indexOf("市") < 5) {
                        county = county.substring(0, county.indexOf("市") + 1);
                    }
                    if (county.split("旗").length > 1) {
                        county = county.substring(0, county.indexOf("旗") + 1);
                    }
                    if (county.split("海域").length > 1) {
                        county = county.substring(0, county.indexOf("海域") + 2);
                    }
                    if (county.split("区").length > 1) {
                        county = county.substring(0, county.indexOf("区") + 1);
                    }
                }
            }
        }
        if("province".equals(detailed)){
            //省
            // return codeMappingMapperStaticPack.getInsuCode(province);
        }else if ("city".equals(detailed)){
            //市
            // return codeMappingMapperStaticPack.getCityInsuCode(city);
        }else if ("county".equals(detailed)){
            //区
            // return codeMappingMapperStaticPack.getCountyInsuCode(city,county);
        }
        return "地址填写有误";
    }



    /**
     *
     * @param Line1 全部地址
     * @return 详细地址
     */
    public static String  getDetailAddress(String Line1) {
        if(StringUtils.isEmpty(Line1)){
            return "";
        }else if(Line1.contains("区")){
            String[] address = Line1.split("区");
            String lastaddress = address.length>1?address[address.length-1]:"";
            return lastaddress.replaceAll("）","");
        }else if(Line1.contains("市")){
            String[] address = Line1.split("市");
            String lastaddress = address.length>1?address[address.length-1]:"";
            return lastaddress.replaceAll("）","");
        } else {
            return "";
        }
    }

    public static void main(String[] args) {
        String Line1 = "浙江省绍兴市嵊州区三江街道xxxx5幢一单元";
        if(StringUtils.isEmpty(Line1)){
            System.out.println(1);
        }else if(Line1.contains("区")){
            String[] address = Line1.split("区");
            String lastaddress = address.length>1?address[address.length-1]:"";
            System.out.println(lastaddress.replaceAll("）",""));
        }else if(Line1.contains("市")){
            String[] address = Line1.split("市");
            String lastaddress = address.length>1?address[address.length-1]:"";
            System.out.println("111"+lastaddress.replaceAll("）",""));
        } else {
            System.out.println(2);
        }

    }
    }
